package elitech.vietnam.weatherfashion.backgroundtask;

import java.io.IOException;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import elitech.vietnam.weatherfashion.MainActivity;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.custom.CommentAdapter;
import elitech.vietnam.weatherfashion.dto.CommentDTO;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import elitech.vietnam.weatherfashion.dto.PostDTO;
import elitech.vietnam.weatherfashion.service.PHPSoapServiceCaller;

public class PostCommentTask extends AsyncTask<String, Integer, Boolean> {

	private PostDTO post;
	private CommentAdapter mAdapter;
	private LinearLayout layout_comments;
	private TextView text_commentcount;
	private TextView text_p_commentcount;
	private MemberDTO user;

	String content;
	int postID;

	public PostCommentTask(Context context, PostDTO post, CommentAdapter mAdapter,
			LinearLayout layout_comments, TextView text_commentcount, TextView text_p_commentcount) {
		this.post = post;
		this.mAdapter = mAdapter;
		this.layout_comments = layout_comments;
		this.text_commentcount = text_commentcount;
		this.text_p_commentcount = text_p_commentcount;
		this.user = GlobalValue.getInstance(context).readUserInfo();
	}

	@Override
	protected Boolean doInBackground(String... params) {
		int membID = user.getId();
		postID = this.post.getId();
		if (params == null)
			return false;
		if (params.length <= 0)
			return false;
		PHPSoapServiceCaller appService = new PHPSoapServiceCaller();
		if (params.length == 1) {
			content = params[0];
			try {
				return appService.comment(membID, postID, content);
			} catch (IOException e) {
				return false;
			}
		} else if (params.length == 2) {
			content = params[0];
			String username = user.getMemberName();
			try {
				return appService.commentOnMemberPost(membID, username,
						post.getStoreId(), postID, post.getContent(), content);
			} catch (IOException e) {
				return false;
			}
		} else
			return false;
	}

	@Override
	protected void onPostExecute(Boolean result) {
		if (!result) {
			try {
				MainActivity activity = (MainActivity) mAdapter.getContext();
				if (activity != null) {
//					AlertDialog.Builder bulder = new AlertDialog.Builder(activity);
//					bulder.setTitle(activity.getString(R.string.confirm_exit)).setMessage(activity.getString(R.string.post_task_failed)).setPositiveButton(activity.getString(R.string.close), null).create()
//							.show();
					Toast.makeText(activity, activity.getString(R.string.post_task_failed), Toast.LENGTH_LONG).show();
				}
			} catch (ClassCastException e) {
				e.printStackTrace();
			}
			return;
		}
		CommentDTO data = new CommentDTO();
		data.setMemberId(user.getId());
		data.setMemberImage(user.getMemberPic());
		data.setMemberName(user.getMemberName());
		data.setCommentContent(content);
		data.setPostId(postID);
		mAdapter.addItem(data, layout_comments, text_commentcount, text_p_commentcount);

		this.post = null;
		this.mAdapter = null;
		this.layout_comments = null;
		this.text_commentcount = null;
	}
}
